const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressInfo: {
      username: '胡儒清',
      phone: 13800000000,
      address: '广东省深圳市龙岗区龙岗区龙岗区左创智慧501室'
    },
    // 预订单详情
    detail: {
      carts: []
    },
    // 总费用
    total_fee: 0,
    expressFee:0,
    discount: 0,
    totalMoney:0,
    // 商品号
    mchid: '1541833561',
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function (options) {
    let preOrderId = options.preOrderId;
    console.log(preOrderId);
    this.getPreOrderDetai(preOrderId);
    this.setData({ preOrderId})
  },

  getPreOrderDetai(preOrderId) {
    let url = '/preOrder/detail';
    app.get(url, { preOrderId }).then(res => {
      let total_fee = res.result.totalMoney/1 + res.result.expressFee/1;
      this.setData({
        detail: res.result,
        total_fee: total_fee,
        expressFee: res.result.expressFee,
        totalMoney: res.result.totalMoney
      })
    }).catch(err => {
      console.log(err);
    })
  },

  submit() {
    // 获取支付需要的信息
    let url = "/order/getPayInfo";

    // 特殊处理
    let { total_fee, discount, expressFee, totalMoney} = this.data;
    let data = {
      expressFee,
      total_fee,
      totalPrice: totalMoney,
      discount,
      preOrderId: this.data.preOrderId,
      addressInfo: this.data.addressInfo
    };
    app
      .post(url, data)
      .then(res => {
        // 实际项目中,在这里调起微信支付
        // wx.requestPayment({
        //   timeStamp: res.timeStamp,
        //   nonceStr: res.nonceStr,
        //   package: res.package,
        //   signType: res.signType,
        //   paySign: res.paySign,
        //   success(res) { },
        //   fail(res) { }
        // })

        let { qrcode, orderId} = res.result;
        wx.navigateTo({
          url: `/pages/pay/pay/pay?qrcode=${qrcode}&orderId=${orderId}&total_fee=${this.data.total_fee}`,
        })
      })
      .catch(err => {
        console.log(err);
      });
  }
});
